﻿using FruitMatching.DataModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace FruitMatching
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private static List<Question> listQuestion;
        private static Question currentQuestion;
        private static int currentQuestionIndex;
        private static Storyboard sbRotation;
        private ImageBrush imgAnswer1 = new ImageBrush();
        private ImageBrush imgAnswer2 = new ImageBrush();
        private ImageBrush imgAnswer3 = new ImageBrush();
        private ImageBrush imgAnswer4 = new ImageBrush();

        public MainPage()
        {
            this.InitializeComponent();
            //this.RegisterName(btnAnswer1.Name, btnAnswer1);
            listQuestion = XmlHelper.GetAllQuestions(@"Data\FruitMatchingDB.xml");
            currentQuestionIndex = 0;
            this.ShowQuestionAtIndex(currentQuestionIndex);
            sbRotation = new Storyboard();
            answer1TrueSB.Completed += answerTrueSB_Completed;
            answer2TrueSB.Completed += answerTrueSB_Completed;
            answer3TrueSB.Completed += answerTrueSB_Completed;
            answer4TrueSB.Completed += answerTrueSB_Completed;
            RotationSB.Completed += RotationSB_Completed;
        }

        void RotationSB_Completed(object sender, object e)
        {
            RotationSB.Stop();
            currentQuestionIndex++;
            if (currentQuestionIndex < listQuestion.Count)
            {
                this.ShowQuestionAtIndex(currentQuestionIndex);
            }
            else
            {
                txtbScore.Text = "Finish";
            }
        }

        void answerTrueSB_Completed(object sender, object e)
        {
            this.ResetItemAt(currentQuestion.RightAnswer);
            currentQuestionIndex++;
            if (currentQuestionIndex < listQuestion.Count)
            {
                this.ShowQuestionAtIndex(currentQuestionIndex);
            }
            else
            {
                txtbScore.Text = "Finish";
            }
        }

        private void MoveItemAt(string i)
        {
            switch (i)
            {
                case "1":
                    answer1TrueSB.Begin();
                    break;
                case "2":
                    answer2TrueSB.Begin();
                    break;
                case "3":
                    answer3TrueSB.Begin();
                    break;
                case "4":
                    answer4TrueSB.Begin();
                    break;
                default:
                    break;
            }
        }

        private void ResetItemAt(string i)
        {
            switch (i)
            {
                case "1":
                    answer1TrueSB.Stop();
                    break;
                case "2":
                    answer2TrueSB.Stop();
                    break;
                case "3":
                    answer3TrueSB.Stop();
                    break;
                case "4":
                    answer4TrueSB.Stop();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnAnswer_Click(object sender, RoutedEventArgs e)
        {      
            if (RotationSB.GetCurrentState() == ClockState.Stopped) 
            {
                Button btn = (Button)sender;                    
                if (currentQuestion.RightAnswer == btn.Tag.ToString())
                {
                    txtbScore.Text = "True";
                    this.PlayAudio("Assets/Sounds/touched.mp3");
                    this.MoveItemAt(currentQuestion.RightAnswer);
                    //sbRotation = this.CreateStoryBoard(btn.Name);
                    //sbRotation.Begin();
                    //Storyboard.SetTargetName(RotationSB, btn.Name);
                    //RotationSB.Begin();
                }
                else
                {
                    txtbScore.Text = "False";
                    //sbRotation = this.CreateStoryBoard(btn.Name);
                    //sbRotation.Begin();
                    //this.MoveItemAt(currentQuestion.RightAnswer);
                    Storyboard.SetTargetName(RotationSB, btn.Name);
                    RotationSB.Begin();
                }
            }
        }

        private void ShowQuestionAtIndex(int i)
        {
            currentQuestion = (Question)listQuestion.ElementAt(i);
            imgFruitPartern.Source = new BitmapImage(new Uri("ms-appx:///" + currentQuestion.ImgFruitParternSrc));
            imgAnswer1.ImageSource = new BitmapImage(new Uri("ms-appx:///" + currentQuestion.ImgAnswer1Src));
            imgAnswer2.ImageSource = new BitmapImage(new Uri("ms-appx:///" + currentQuestion.ImgAnswer2Src));
            imgAnswer3.ImageSource = new BitmapImage(new Uri("ms-appx:///" + currentQuestion.ImgAnswer3Src));
            imgAnswer4.ImageSource = new BitmapImage(new Uri("ms-appx:///" + currentQuestion.ImgAnswer4Src));
            btnAnswer1.Background = imgAnswer1;
            btnAnswer2.Background = imgAnswer2;
            btnAnswer3.Background = imgAnswer3;
            btnAnswer4.Background = imgAnswer4;
        }

        private Storyboard CreateStoryBoard(string objName)
        {
            Storyboard sb = new Storyboard();
            sb = RotationSB;
            Storyboard.SetTargetName(sb, objName);
            sb.Completed += OnStoryboardCompleted;
            return sb;
        }

        void OnStoryboardCompleted(object sender, object e)
        {
            sbRotation = (Storyboard)sender;
            sbRotation.Stop();
            
            currentQuestionIndex++;
            if (currentQuestionIndex < listQuestion.Count)
            {
                this.ShowQuestionAtIndex(currentQuestionIndex);
            }
            else
            {
                txtbScore.Text = "Finish";
            }
        }

        private void PlayAudio(string uri)
        {
            MediaElement me = new MediaElement() { AudioCategory=AudioCategory.SoundEffects, Source = new Uri(this.BaseUri, uri) };
            this.rootGrid.Children.Add(me);
            me.Play();
        }
    }
}
